package com.jielin.handling.service.user.impl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jielin.handling.bean.user.User;
import com.jielin.handling.bean.user.UserSession;
import com.jielin.handling.mapper.user.UserMapper;
import com.jielin.handling.redis.RedisKey;
import com.jielin.handling.redis.RedisTemplateX;
import com.jielin.handling.service.user.UserSessionService;

import lombok.extern.slf4j.Slf4j;

/**
 * 类名称：UserSessionServiceImpl
 * 创建人：zhanglongdeng@163.com
 * 创建时间：2019年3月18日 下午4:03:10   
 * 类描述：当前登录用户SESSION获取
 */
@Service
@Slf4j
public class UserSessionServiceImpl implements UserSessionService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private RedisTemplateX redisTemplateX;
	
	@Override
	public UserSession getUserSession(HttpServletRequest request) {
		String userId = (String) redisTemplateX.get(request.getHeader(RedisKey.USER_TOKEN));
		log.info("userSession userId===>{}",userId);
		UserSession userSession = (UserSession) redisTemplateX.get(userId);
		if(userSession==null) {
			User user = userMapper.selectUserById(userId);
			userSession = UserSession.convert(user);
			redisTemplateX.set(userId, userSession);
		}
		log.info("userSession bean===>{}",userSession);
		return userSession;
	}

}
